#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>

void* mythread(void* arg) {
    int n = *(int*)arg;

    printf("[%d] 子线程, 其所在进程: pid = [%d], 线程 id = [%ld]\n", n, getpid(), pthread_self());
    sleep(20);
}

int main()
{
    // int pthread_create(pthread_t *thread,  const pthread_attr_t *attr,   void *(*start_routine) (void *),  void *arg);
    pthread_t thr[5];
    int ret;
    int arr[5];

    for(int i = 0; i < 5; i++) {
        arr[i] = i;
        ret = pthread_create(&thr[i], NULL, mythread, &arr[i]);
        if(ret != 0) {
            printf("pthread_create err: [%s]\n", strerror(ret));
            return -1;
        }
    }

    printf("主线程, 其所在进程: pid = [%d], 主线程 id = [%ld]\n", getpid(), pthread_self());

    sleep(1);   // 确保子线程能执行
    return 0;
}

